package com.lanche.test.controller;


import com.google.gson.Gson;
import com.lanche.test.config.MailService;
import com.lanche.test.myPack.Mert;
import com.lanche.test.myPack.Shoes;
import com.lanche.test.myPack.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Random;

@CrossOrigin(origins = "http://localhost:8181", maxAge = 3600)
@Controller
@ResponseBody
public class MailController {
    @Autowired
    private MailService mailService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/register")
    public String getCheckCode(@RequestBody String tmp){
        Gson gson=  new Gson();
        Mail mail=gson.fromJson(tmp,Mail.class);

        System.out.println(mail.getEmail());
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String message = "您的注册验证码为："+checkCode;
        try {
            mailService.sendSimpleMail(mail.getEmail(), "鞋靴助手平台--注册验证码", message);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(checkCode);
        return checkCode;
    }

    @RequestMapping("/register/checkUser")
    public boolean checkUser(@RequestBody String tmp){
        Gson gson=  new Gson();
        User user=gson.fromJson(tmp,User.class);
        System.out.println(tmp+": "+user.getUsername());
        String sql=String.format("select count(*) as num from user where username='%s'",user.getUsername());
        Map<String,Object> mp=jdbcTemplate.queryForMap(sql);
        System.out.println(mp);
        if((long)mp.get("num")>0) return false;
        return true;
    }

    @RequestMapping("/register/checkMert")
    public boolean checkMert(@RequestBody String tmp){
        Gson gson=  new Gson();
        Mert mert=gson.fromJson(tmp,Mert.class);
        System.out.println(tmp+": "+mert.getMt_name());
        String sql=String.format("select count(*) as num from mert where mt_name='%s'",mert.getMt_name());
        Map<String,Object> mp=jdbcTemplate.queryForMap(sql);
        System.out.println(mp);
        if((long)mp.get("num")>0) return false;
        return true;
    }

    @RequestMapping("/register/addMert")
    public void addMert(@RequestBody String tmp){
        Gson gson=  new Gson();
        Mert mert=gson.fromJson(tmp,Mert.class);
        System.out.println(mert.getMt_name());
        String sql=String.format("insert into mert values('%s','%s')",mert.getMt_name(),mert.getMt_pwd());
        System.out.println(sql);
        jdbcTemplate.update(sql);
    }

    @RequestMapping("/register/addUser")
    public void addUser(@RequestBody String tmp){
        Gson gson=  new Gson();
        User user=gson.fromJson(tmp,User.class);
        System.out.println(user.getUsername());
        String sql=String.format("insert into user values('%s','%s')",user.getUsername(),user.getUser_pwd());
        System.out.println(sql);
        jdbcTemplate.update(sql);
    }
}

class Mail{
    private String email;

    public Mail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

